// @ts-ignore
import dayjs from "dayjs";
import { logger } from "./logger";

export const getCurrentTime = () => dayjs().format("YYYY-MM-DD HH:mm:ss");

export const foramtTime = (format) => dayjs(format);

// 判断是否节假日（支持中国）
export async function isHoliday(date = new Date()) {
  const yyyyMMdd = date.toISOString().slice(0, 10).replace(/-/g, '');
  const url = `https://api.apihubs.cn/holiday/get?date=${yyyyMMdd}&cn=1&size=1`;

  try {
    const res = await fetch(url);
    const json = await res.json();
    const { data } = json;

    if (data.list && data.list.length > 0) {
      const item = data.list[0];
      return item.type !== 0;
    }
  } catch (err) {
    logger('节假日判断接口失败，默认当作工作日处理');
  }

  return false;
}
